package com.touch.client;
import javax.swing.JOptionPane;

import com.touch.client.forms.MainMenuControls;
import com.touch.core.NetConstants;
import com.touch.core.avatar.Avatar;
import com.touch.core.net.*;
import com.touch.core.script.ByteArray;
import com.touch.core.script.ByteEncoder;
public class ClientSocketEvents implements NetListener
{
	public void messageReceived(Object host, byte[] message) 
	{		
		if(NetConstants.compare(NetConstants.REQUEST_CONNECTION, message))
			processConnected();
		else if(NetConstants.compare(NetConstants.FAILED_MAKE_NEW_ACCOUNT, message))
			processFailedNewAccount();
		else if(NetConstants.compare(NetConstants.REQUEST_NEW_ACCOUNT, message))
			processNewAccount();
		else if(NetConstants.compare(NetConstants.REQUEST_LOGIN, message))
			processLogin(message);
		else if(NetConstants.compare(NetConstants.ACCOUNT_BANNED, message))
			JOptionPane.showConfirmDialog(null, "This Account was banned!", "Banned", JOptionPane.OK_OPTION);					
		else if(NetConstants.compare(NetConstants.FAILED_LOGIN, message))
			JOptionPane.showConfirmDialog(null, "Either this account does not exist, or the password and/or username are wrong.", "Error", JOptionPane.OK_OPTION);
		else if(NetConstants.compare(NetConstants.RANDOMIZE_STATS, message))
			processRandomizedStats(message);
		else if(NetConstants.compare(NetConstants.REQUEST_CLASS_INFO, message))
			processClassList(message);
		
	}
	public void processConnected()
	{
		System.out.println("Connected!");
		ClientControls.CONNECTED = true;
	}
	public void processFailedNewAccount()
	{
		JOptionPane.showConfirmDialog(
				null,"There was an error in creating this account!", "Error", JOptionPane.OK_OPTION);
	}
	public void processNewAccount()
	{
		JOptionPane.showConfirmDialog(null, "Account Created!", "Success", JOptionPane.OK_OPTION);
		MainMenuControls.New_Account_Events.fNewAccount.setVisible(false);	
	}
	public void processLogin(byte[] message)
	{
		ByteArray data = new ByteArray(message);
		data = data.subArray(4, data.length());
		int charnum = data.subArray(0, 4).toInt();
		data = data.subArray(4, data.length());
		ClientControls.characters[charnum] = new Avatar(data);
		MainMenuControls.Char_Select_Events.bChar[charnum].setCaption(ClientControls.characters[charnum].name);
		MainMenuControls.Char_Select_Events.fCharSelect.setVisible(true);
		MainMenuControls.Login_Events.fLogin.setVisible(false);		
	}
	public void processRandomizedStats(byte[] message)
	{
		message = ByteEncoder.getSubByteArray(message, 4, message.length - 1);
		ByteArray messageArray = new ByteArray(message);
		
		int stat1 = messageArray.subArray(0, 4).toInt();
		int stat2 = messageArray.subArray(4, 8).toInt();
		int stat3 = messageArray.subArray(8, 12).toInt();
		int stat4 = messageArray.subArray(12, 16).toInt();
		int hp = messageArray.subArray(16, 20).toInt();
		int mp = messageArray.subArray(20, 24).toInt();
		int sp = messageArray.subArray(24, 28).toInt();
		
		MainMenuControls.New_Character_Events.setStats(stat1, stat2, stat3, stat4, hp, mp, sp);
	}
	public void processClassList(byte[] message)
	{
		message = ByteEncoder.getSubByteArray(message, 4, message.length - 1);
		String sMessage = ByteEncoder.byteArrayToString(message);
		String[] classes = sMessage.split(new String(NetConstants.SEP_SEQ));
		for(String c: classes)
			ClientControls.Classes.addElement(c);		
	}
	public static final byte[] NEW_ACCOUNT_PACKET(byte[] name, byte[] password, byte[] sex)
	{
		return ByteEncoder.appendToByteArray(
			NetConstants.REQUEST_NEW_ACCOUNT, name, NetConstants.SEP_SEQ, password, NetConstants.SEP_SEQ, sex);
	}
	public static final byte[] LOGIN_PACKET(byte[] name, byte[] password)
	{
		return ByteEncoder.appendToByteArray(
			NetConstants.REQUEST_LOGIN.clone(), name, NetConstants.SEP_SEQ, password);
	}

}
